using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.Package;
using Microsoft.VisualStudio.TextManager.Interop;
using NBusiness.CodeDom.Compiler;
using NBusiness.ESharp.Compiler;

namespace NBusiness.VisualStudio
{
    internal class ESharpDeclarations : Declarations
    {
        List<ESharpDeclaration> _declarations = new List<ESharpDeclaration>();
        private TextSpan _commitSpan;

        public ESharpDeclarations(params ESharpDeclaration[] declarations)
        {
            _declarations.AddRange(declarations);
        }

        public override int GetCount()
        {
            return _declarations.Count;
        }

        public override string GetDescription(int index)
        {
            string description = "";
            if (index >= 0 && index < _declarations.Count)
            {
                description = _declarations[index].Description;
            }
            return description;
        }

        public override string GetDisplayText(int index)
        {
            string text = "";
            if (index >= 0 && index < _declarations.Count)
                text = _declarations[index].Title;

            return text;
        }

        public override int GetGlyph(int index)
        {
            int glyph = 0;

            //The following constants are the index of the various glyphs in the ressources of Microsoft.VisualStudio.Package.LanguageService.dll

            if (index >= 0 && index < _declarations.Count)
            {
                if (_declarations[index].Type == ESharpEntityElementTypes.AccessAllow ||
                    _declarations[index].Type == ESharpEntityElementTypes.AuthorizeAllow ||
                    _declarations[index].Type == ESharpEntityElementTypes.AuthorizeMethod ||
                    _declarations[index].Type == ESharpEntityElementTypes.AccessMethod ||
                    _declarations[index].Type == ESharpEntityElementTypes.RelationshipType ||
                    _declarations[index].Type == ESharpEntityElementTypes.ActionWhenType)
                {
                    glyph = (int)IconImageIndex.EnumMember;
                }
                else if (_declarations[index].Type == ESharpEntityElementTypes.FieldType)
                {
                    glyph = (int)IconImageIndex.Base;
                }
                else if (_declarations[index].Type == ESharpEntityElementTypes.RelationshipField ||
                    _declarations[index].Type == ESharpEntityElementTypes.AccessField ||
                    _declarations[index].Type == ESharpEntityElementTypes.ValidateProperty ||
                    _declarations[index].Type == ESharpEntityElementTypes.RelationshipAssignTo)
                {
                    glyph = (int)IconImageIndex.Field;
                }
                else if (_declarations[index].Type == ESharpEntityElementTypes.EntityTemplateType)
                {
                    glyph = (int)IconImageIndex.Intrinsic;
                }
                else if (_declarations[index].Type == ESharpEntityElementTypes.RelationshipWith ||
                    _declarations[index].Type == ESharpEntityElementTypes.RelationshipEntity)
                {
                    glyph = (int)IconImageIndex.Class;
                }
                else
                {
                    glyph = (int)IconImageIndex.Error;

                }
            }
            return glyph;
        }

        public override string GetName(int index)
        {
            string name = string.Empty;
            if (index >= 0 && index < _declarations.Count)
            {
                ESharpDeclaration item = _declarations[index];
                if (item.Type == ESharpEntityElementTypes.FamilyName)
                {
                    name = _declarations[index].Shortcut;
                }
                else
                {
                    name = item.Title;
                }
            }
            return name;
        }

        public override string OnCommit(IVsTextView textView, string textSoFar, char commitCharacter, int index, ref TextSpan initialExtent)
        {
            _commitSpan = initialExtent;

            return base.OnCommit(textView, textSoFar, commitCharacter, index, ref initialExtent);
        }

        public void Add(ESharpDeclaration declaration)
        {
            _declarations.Add(declaration);
        }

        public void AddRange(IEnumerable<ESharpDeclaration> declarations)
        {
            _declarations.AddRange(declarations);
        }

        internal void Sort()
        {
            _declarations.Sort();
        }
    }
}
